package learn_net;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class T1 {
	
	public static void main(String[] args) {
		System.out.println(requestOtherSystem());
	}
	
	private static String requestOtherSystem() {
		try {
			URL url = new URL("http://localhost:9999/demo");
			URLConnection cc = url.openConnection();
			System.out.println(cc); // sun.net.www.protocol.http.HttpURLConnection:http://localhost:9999/demo
			HttpURLConnection con = (HttpURLConnection) cc;
			con.setRequestMethod("GET");
			con.setConnectTimeout(5000);
			con.setReadTimeout(5000);

			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer content = new StringBuffer();
			while ((inputLine = in.readLine()) != null) {
				content.append(inputLine);
			}
			in.close();
			con.disconnect();

			return content.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "ERROR";
		}
	}
	
}
